package org.jiang.springbootdemo.demos.web.test.sort;

public class BinarySearch {

    public static Integer binarySearch(Integer[] nums, Integer left, Integer right, Integer key) {
        if (left > right) {
            return -1;
        }
        while (true) {
            Integer mid = (right + left) / 2;
            if (key > nums[mid]) left = mid + 1;
            else if (key < nums[mid]) right = mid - 1;
            else return mid;
        }
    }

    public static void main(String[] args) {
        Integer[] nums = {1,3,5,6,7,9};
        System.out.println(binarySearch(nums, 0, nums.length - 1, 7));
    }
}
